window._CHESSJFUI = {

    _MAINLABEL:null,

    _POOLINDEX:-1,

    _ISVIP:false,

    _ONCEPRICE:1000,//单抽价格

    _TENPRICE:9000,//十抽价格

    showJFUI:function(parentNode){

        var uiObj = _GRM.getNodeObj('duihuangLabel');
        var mainLabel = uiObj.node;
        _CHESSJFUI._MAINLABEL = mainLabel;

        if(uiObj.isnew){
            _CHESSJFUI.initButton(mainLabel);
        }

        _CHESSJFUI.showCode(mainLabel);
        _CHESSJFUI.showHome();
        _CHESSJFUI.refreshHome();
        _HTTP.getJf();
        parentNode.addChild(mainLabel);
        return mainLabel;

    },

    initButton:function(mainLabel){

        //var landmaskNode = mainLabel.getChildByName('landlabel').getChildByName('mask');//背景遮罩
        var landbtnNode = mainLabel.getChildByName('landlabel').getChildByName('adbtn');//背景遮罩
        landbtnNode.on(cc.Node.EventType.TOUCH_START, function(){
            //_GRM.putNode(this);
            _QD.loginByQd('bycode');
        }, mainLabel);


        var jfmaskNode = mainLabel.getChildByName('mask');//背景遮罩
        var tapNode = mainLabel.getChildByName('tab');
        var rewardNode = mainLabel.getChildByName('gie');
        var rightNode = mainLabel.getChildByName('rightLabel');

        var poolbtn = tapNode.getChildByName('pool');//奖池切换
        var rulebtn = tapNode.getChildByName('rule');//公示信息

        var packageNode = rightNode.getChildByName('background').getChildByName('reward');
        var homeNode = rightNode.getChildByName('background').getChildByName('home');

        var adbtn = homeNode.getChildByName('adbtn');//广告按钮
        var codebtn = homeNode.getChildByName('codebtn');//兑换按钮
        var homegoods = homeNode.getChildByName('goods').getChildren();//奖池按钮

        var rewardgoods = packageNode.getChildByName('goods').getChildren();//卡包按钮
        var singlebtn = packageNode.getChildByName('btn').getChildByName('single');//单抽按钮
        var tenbtn = packageNode.getChildByName('btn').getChildByName('ten');//连抽按钮

        var rewardMask = rewardNode.getChildByName('mask');//奖励遮罩
        var gailvMask = mainLabel.getChildByName('gailv').getChildByName('mask');

        jfmaskNode.on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
            _CHESSJFUI._MAINLABEL = null;
        }, mainLabel);
        gailvMask.on(cc.Node.EventType.TOUCH_START, function(){
            mainLabel.getChildByName('gailv').active = false;
        }, mainLabel);
        poolbtn.on(cc.Node.EventType.TOUCH_START, function(){
            console.log('奖池切换');
            var rightNode = this.getChildByName('rightLabel');
            var packageNode = rightNode.getChildByName('background').getChildByName('reward');
            var homeNode = rightNode.getChildByName('background').getChildByName('home');
            packageNode.active = false;
            homeNode.active = true;
        }, mainLabel);
        rulebtn.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('公示信息');
            mainLabel.getChildByName('gailv').active = true;;
        }, mainLabel);

        if(_QD.hasAdLb()){
            adbtn.active = true;
            adbtn.on(cc.Node.EventType.TOUCH_START, function(){
                //console.log('广告按钮');

                _MWX.showLbVideo();

            }, mainLabel);
        }else{
            adbtn.active = false;
        }

        codebtn.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('兑换按钮');
            var rightNode = _CHESSJFUI._MAINLABEL.getChildByName('rightLabel');
            var background = rightNode.getChildByName('background');
            var homeNode = background.getChildByName('home');
            var editbox = homeNode.getChildByName('editbox')
            var code = editbox.getComponent(cc.EditBox).string;
            if(code)
                _HTTP.buyCode(code);
            else
                _TIPS.showMessage('兑换码不能为空!');
        }, mainLabel);
        singlebtn.on(cc.Node.EventType.TOUCH_START, function(){
        }, mainLabel);
        tenbtn.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('连抽按钮');
        }, mainLabel);
        rewardMask.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('奖励遮罩');
            var rewardNode = this.getChildByName('gie');
            rewardNode.active = false;
        }, mainLabel);

        for(var i=0;i<homegoods.length;i++){
            var poolgoods = homegoods[i];
            poolgoods.poolindex = i;
            poolgoods.mainLabel = mainLabel;
            poolgoods.on(cc.Node.EventType.TOUCH_START, function(){
                console.log('奖池:',this.poolindex);
                _CHESSJFUI._POOLINDEX = this.poolindex;
                var rightNode = this.mainLabel.getChildByName('rightLabel');
                var packageNode = rightNode.getChildByName('background').getChildByName('reward');
                var homeNode = rightNode.getChildByName('background').getChildByName('home');
                packageNode.active = true;
                homeNode.active = false;
                _CHESSJFUI.refreshPackages();
            }, poolgoods);
        }

        for(var i=0;i<rewardgoods.length;i++){
            var poolgoods = rewardgoods[i];
            poolgoods.packageindex = i;

            poolgoods.on(cc.Node.EventType.TOUCH_START, function(){
                var config = window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss[this.packageindex];
                console.log(config);
                var itemname = config.name;
                if(_NPC[itemname]){
                    _NPCDL.initUi(_MAP.node,{sence:_STYPE._FIGHTSENCE,
                        roleArr:[{value:itemname,config:_NPC[itemname]}],
                        roleIndex:0
                        });
                }else {
                    _MGD.showGoodsUi(_MAP.node,
                        _IOF.createObjByName(itemname),
                        {pos:_MGD._POSTUJ});
                }
            }, poolgoods);

            poolgoods.getChildByName('dhbtn').on(cc.Node.EventType.TOUCH_START, function(){
                console.log('卡包:',this.packageindex);

                if(
                    window.JFBOSSPER[_CHESSJFUI._POOLINDEX]
                    &&window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss
                    &&window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss[this.packageindex]
                ){

                    var config = window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss[this.packageindex];

                    console.log(config);

                    if(_CHESSJFUI._ISVIP){
                        _CHESSJFUI.getRewardOfConfig(config);
                    }else{
                        if(_LOCALJF.cutJIFEN(config.cut)){
                            _CHESSJFUI.getRewardOfConfig(config);
                            _HTTP.getJf();
                        }else{
                            _HTTP.cutJf(config.cut,config);
                        }
                    }

                }

            }, poolgoods);
        }

    },

    getRewardOfConfig:function(config){

        var rewardName = config.name;
        var rewardnmb = config.nmb;
        if(rewardName=="金币"){
            window.gameData.gold+=config.nmb;
            _MAP.refreshInfoLabel();
        }else if(_SKILL[rewardName]
            ||_SKILLBD[rewardName]
            ||_FIGHTITEM[rewardName]
        ){

            var obj = _IOF.createObjByName(rewardName,null);
            obj.nmb = rewardnmb;
            _GAMEDATAHELPER.pushItem(obj);

        }else if(_EQUIP[rewardName]){
            var dropEquip = _IOF.createEquip(rewardName,{qua:"SS"});
            _MCFC.initEquipXw(dropEquip);
            _GAMEDATAHELPER.pushItem(dropEquip);

        }else if(_NPC[rewardName]){

            //var tmpnpc = _CROF.getRandomMainRole(false,rewardName,null);
            var tmpnpc = _MCFC.createNpcRole(rewardName,{lv:5,nodz:1});
            window.gameData.playerTeamBack.unshift(tmpnpc);


        }

        _TIPS.showMessage('兑换成功!获得:'+rewardName+"*"+rewardnmb);
    },

    refreshJFInfo:function(isvip,jfnmb){
        if( _CHESSJFUI._MAINLABEL){
            var rightNode = _CHESSJFUI._MAINLABEL.getChildByName('rightLabel');
            var homeNode = rightNode.getChildByName('background').getChildByName('home');
            var jflabel = homeNode.getChildByName('Label');

            if(isvip){
                jflabel.getComponent(cc.Label).string="您已经是尊贵的VIP用户,可以无限制兑换道具";
            }else {
                var jfmessage = "剩余积分:"+jfnmb;
                if(_LOCALJF.getJIFEN()!=0){
                    jfmessage+=" 本地积分:"+_LOCALJF.getJIFEN();
                }
                jflabel.getComponent(cc.Label).string=jfmessage;
            }
            _CHESSJFUI.refreshHome();
        }
    },

    showCode:function(mainLabel){

        var currentCode = _QD.getcodeNodeName();
        var codearrs = mainLabel.getChildByName('codes').getChildren();
        for(var i=0;i<codearrs.length;i++){
            if(codearrs[i].getName()==currentCode){
                codearrs[i].active = true;
            }else{
                codearrs[i].active = false;
            }
        }
        
    },

    showHome:function(){

        var rightNode = _CHESSJFUI._MAINLABEL.getChildByName('rightLabel');
        var packageNode = rightNode.getChildByName('background').getChildByName('reward');
        var homeNode = rightNode.getChildByName('background').getChildByName('home');
        packageNode.active = false;
        homeNode.active = true;
    },


    refreshHome:function(){
        var rightNode = _CHESSJFUI._MAINLABEL.getChildByName('rightLabel');
        var homeNode = rightNode.getChildByName('background').getChildByName('home');
        var homegoods = homeNode.getChildByName('goods').getChildren();//奖池按钮
        for(var i=0;i<homegoods.length;i++){
            homegoods[i].active = false;
        }
        for(var i=0;i<window.JFBOSSPER.length;i++){
            if(homegoods[i]){
                homegoods[i].active = true;
                homegoods[i].getChildByName('nmb').active = false;
                homegoods[i].getChildByName('numbei').getComponent(cc.Label).string = window.JFBOSSPER[i].label;
            }
        }

        var landlabel = _CHESSJFUI._MAINLABEL.getChildByName('landlabel');//背景遮罩
        if(!_HQM._USERNAME){
            landlabel.active = true;
        }else{
            landlabel.active = false;
        }
    },

    refreshPackages:function(){

        var poolArr = window.JFBOSSPER[_CHESSJFUI._POOLINDEX];
        var rightNode = _CHESSJFUI._MAINLABEL.getChildByName('rightLabel');
        var packageNode = rightNode.getChildByName('background').getChildByName('reward');
        var rewardgoods = packageNode.getChildByName('goods').getChildren();//卡包按钮
        for(var i=0;i<rewardgoods.length;i++){
            if(poolArr&&poolArr.boss[i]){
                var poolconf = poolArr.boss[i];
                rewardgoods[i].active = true;
                //rewardgoods[i].color = new cc.color(_CS.qua[poolconf.qua].color);
                rewardgoods[i].getChildByName('nmb').getComponent(cc.Label).string = poolconf.cut;
                rewardgoods[i].getChildByName('numbei').getComponent(cc.Label).string = poolconf.name+"*"+poolconf.nmb;
            }else{
                rewardgoods[i].active = false;
            }
        }

    },

    randomRewardOnce:function(){

        if(
            window.JFBOSSPER[_CHESSJFUI._POOLINDEX]
            &&window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss
            &&window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss.length>0
        ){
            var poolArr = window.JFBOSSPER[_CHESSJFUI._POOLINDEX].boss;
            var randomNmb = Math.random();
            var randomBoss = null;
            var randomType = null;
            for(var i=0;i<poolArr.length;i++){
                var tempboss = poolArr[i];
                if(
                    randomNmb>=tempboss.qj[0]
                    &&randomNmb<tempboss.qj[1]
                ){
                    randomBoss = tempboss;
                    break;
                }
            }

            if(randomBoss){
                randomNmb = Math.random();
                for(var key in window.JFCOMMONPER){
                    var tempType = window.JFCOMMONPER[key];
                    if(
                        randomNmb>=tempType.qj[0]
                        &&randomNmb<tempType.qj[1]
                    ){
                        randomType = key;
                        break;
                    }
                }
            }

            if(
                randomBoss
                &&randomType
            ){

                if(!randomBoss.equip
                    ||!randomBoss.skill
                ){
                    var xw = window._JFLEVEL[randomBoss.qua].xw;
                    var bossObj = _CROF.createNpcRole(randomBoss.name,{dzlevel:0,lv:1000,chapterLv:xw,chapter:null});
                    randomBoss.equip=[];
                    randomBoss.skill=[];
                    if(bossObj.skill){
                        for(var i=0;i<bossObj.skill.length;i++){
                            var skillname = bossObj.skill[i];
                            if(
                                _SKILLBD[skillname]
                                &&!_SKILLBD[skillname].skilltype
                                &&!_SKILLBD[skillname].exc
                                &&(
                                    !_SKILLBD[skillname].bossflag
                                    ||_SKILLBD[skillname].bossflag==randomBoss.name
                                )
                            ){
                                randomBoss.skill.push(skillname);
                            }else if(_SKILL[skillname]
                                &&(
                                    !_SKILL[skillname].bossflag
                                    ||_SKILL[skillname].bossflag==randomBoss.name
                                )){
                                randomBoss.skill.push(skillname);
                            }
                        }
                    }
                    if(bossObj.equip){
                        for(var i=0;i<bossObj.equip.length;i++){
                            randomBoss.equip.push(bossObj.equip[i].type);
                        }
                    }
                }

                if(key=="gold"){
                    var goldnmb = window._JFLEVEL[randomBoss.qua].gold;
                    return {qua:randomBoss.qua,name:randomBoss.name,type:key,value:goldnmb};
                }else if(key=="wood"){
                    var woodnmb = window._JFLEVEL[randomBoss.qua].wood;
                    return {qua:randomBoss.qua,name:randomBoss.name,type:key,value:woodnmb};
                }else if(key=="equip"){
                    var dzmin = window._JFLEVEL[randomBoss.qua].dzmin;
                    var dzmax = window._JFLEVEL[randomBoss.qua].dzmax;
                    var dz = dzmin+Math.floor((dzmax-dzmin)*Math.random());
                    return {dz:dz,qua:randomBoss.qua,name:randomBoss.name,type:key,value:_CU.randomObj(randomBoss.equip)};
                }else if(key=="skill"){
                    if(randomBoss.skill.length<=0){
                        //console.log(randomBoss.name,'没有技能');
                        return {qua:randomBoss.qua,name:randomBoss.name,type:key,value:_FIGHTITEMHELPER.randomItemByLevel(randomBoss.qua)};
                    }
                    return {qua:randomBoss.qua,name:randomBoss.name,type:key,value:_CU.randomObj(randomBoss.skill)};
                }else if(key=="part"){
                    return {qua:randomBoss.qua,name:randomBoss.name,type:key};
                }else if(key=="item"){
                    return {qua:randomBoss.qua,name:randomBoss.name,type:key,value:_FIGHTITEMHELPER.randomItemByLevel(randomBoss.qua)};
                }
            }
        }

        return null;
    },

}